//
//  HWRecommentCell.m
//  JWC
//
//  Created by lanou on 16/4/13.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWRecommentCell.h"
#import "HWUserModel.h"
#import "HWContentImageView.h"
#import "HWContentVideoView.h"
#import "HWContentVoiceView.h"
#import "HWCommentViewController.h"
#import "UIImageView+Webcache.h"
#import "UIImageView+Header.h"


@interface HWRecommentCell ()

@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *creatTime;
@property (weak, nonatomic) IBOutlet UIButton *more;
@property (weak, nonatomic) IBOutlet UIButton *comment;
@property (weak, nonatomic) IBOutlet UIButton *cai;
@property (weak, nonatomic) IBOutlet UIButton *ding;
@property (weak, nonatomic) IBOutlet UIButton *share;
@property (weak, nonatomic) IBOutlet UILabel *hotComment;
@property (weak, nonatomic) IBOutlet UILabel *text;
@property (weak, nonatomic) IBOutlet UIView *hotCommentView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textHeight;

@property (nonatomic, strong)HWContentImageView *pictureView;
@property (nonatomic, strong)HWContentVideoView *videoView;
@property (nonatomic, strong)HWContentVoiceView *voiceView;
@end

@implementation HWRecommentCell

- (void)prepareForReuse
{
    [super prepareForReuse];
    [_videoView reset];
    [_voiceView reset];
}

+ (instancetype)cell
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (void)setTopFrame:(HWFrame *)topFrame
{
    _topFrame = topFrame;
    HWRecommendModel *model = topFrame.model;
    
    [self.headImage setHeader:model.u.header.firstObject];
    self.name.text = model.u.name;
    self.creatTime.text = model.passtime;
    self.text.text = model.text;
    CGFloat modelH = [model.text boundingRectWithSize:CGSizeMake(292, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
    self.textHeight.constant = modelH;
    
    
    [self.ding setTitle:model.up forState:UIControlStateNormal];
    [self.cai setTitle:[NSString stringWithFormat:@"%ld", (long)model.down] forState:UIControlStateNormal];
    [self.share setTitle:model.forward forState:UIControlStateNormal];
    [self.comment setTitle:model.comment forState:UIControlStateNormal];
    
    // 热门评论
    if (model.top_comment)
    {
        self.hotCommentView.hidden = NO;
        CGFloat modelH = [model.top_comment.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        _viewHeight.constant = modelH + 30;
        self.hotComment.text = [NSString stringWithFormat:@"%@ : %@", model.top_comment.u.name, model.top_comment.content];
    }
    else
    {
        self.hotCommentView.hidden = YES;
    }
    
    
    if ([model.type isEqualToString:@"image"] || [model.type isEqualToString:@"gif"])
    {
        self.pictureView.model = model;
        self.pictureView.frame = topFrame.contentViewFrame;
        self.pictureView.hidden = NO;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }
    else if ([model.type isEqualToString:@"audio"])
    {
        self.voiceView.model = model;
        self.voiceView.frame = topFrame.contentViewFrame;
        self.voiceView.hidden = NO;
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
    }
    else if ([model.type isEqualToString:@"video"])
    {
        self.videoView.model = model;
        self.videoView.frame = topFrame.contentViewFrame;
        self.videoView.hidden = NO;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
    }
    else
    {
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }
}

//重新设置尺寸
-(void)setFrame:(CGRect)frame {
    CGFloat inset = 10;
    frame.size.height -= inset;
    frame.origin.y += inset;
    [super setFrame:frame];
}

- (IBAction)more:(UIButton *)sender
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *save = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *report = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:report];
    [alertController addAction:cancel];
    [alertController addAction:save];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

// 评论按钮
- (IBAction)comment:(UIButton *)sender
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:self.topFrame forKey:@"topFrame"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"commentClick" object:nil userInfo:dic];
    
}
// 赞
- (IBAction)dingBtn:(UIButton *)sender
{
    HWRecommendModel *model = self.topFrame.model;
    NSInteger count = [model.up integerValue];
    [self.ding setTitle:[NSString stringWithFormat:@"%ld", (long)count + 1] forState:UIControlStateNormal];
    [self.ding setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}


// 踩
- (IBAction)caiBtn:(UIButton *)sender
{
    HWRecommendModel *model = self.topFrame.model;
    [self.cai setTitle:[NSString stringWithFormat:@"%ld", (long)model.down + 1] forState:UIControlStateNormal];
    [self.cai setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}


// 分享
- (IBAction)shareBtn:(UIButton *)sender
{
    NSLog(@"分享");
}



//-(void)dealloc {
//    
//    [[NSNotificationCenter defaultCenter]removeObserver:self forKeyPath:@"topFrame"];
//}

#pragma mark - getter and setter
- (HWContentImageView *)pictureView
{
    if (!_pictureView) {
        _pictureView = [HWContentImageView imageView];
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

-(HWContentVideoView *)videoView
{
    if (!_videoView) {
        _videoView = [HWContentVideoView videoView];
        [self.contentView addSubview:_videoView];
    }
    return _videoView;
}

- (HWContentVoiceView *)voiceView
{
    if (!_voiceView) {
        _voiceView = [HWContentVoiceView voiceView];
        [self.contentView addSubview:_voiceView];
    }
    return _voiceView;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
